package com.qf.day11;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class MyCollection<E> implements Collection<E> {

    private Object[] data = new Object[10];

    private int count = 0;


    @Override
    public int size() {
        return count;
    }

    @Override
    public boolean isEmpty() {
        return count==0;
    }

    @Override
    public boolean contains(Object o) { //判断是否包含o
        if(count==0){
            return false;
        }
        for (int i = 0; i < count; i++) {
            if(data[i].equals(o)){
                return true;
            }
        }
        return false;
    }

    @Override
    public Iterator<E> iterator() {
       return new Iterator<E>() { //匿名内部类
           private int index = 0;

           @Override
           public boolean hasNext() {
               return count > index;

           }

           @Override
           public E next() {
               return (E) data[index++];
           }
       };
    }

    @Override
    public Object[] toArray() {
        Object[] arr = new Object[data.length];
         System.arraycopy(data,0,arr,0,count);
         return arr;
    }

    @Override
    public <T> T[] toArray(T[] a) {
        System.arraycopy(data,0,a,0,count);
        return a;
    }

    private void expansion(Object[] arr) {
        arr  = Arrays.copyOf(arr,arr.length*2);

    }
    @Override
    public boolean add(E e) {

        // 如何保存传入的对象？
        // 放到数组的什么位置呢？ 声明一个实例变量，存放应该插入元素的位置
        // 同时这个变量也是集合中的元素的个数
        data[count++]=e;
        if(count == data.length){
            expansion(data);
        }
        return true;
    }

    @Override
    public boolean remove(Object o) {
        if(count == 0){
            return false;
        }
        for (int i = 0; i < count; i++) {
            if(data[i]==o){
                for (int j = i; j < count-1; j++) {
                    data[j] = data[j+1];
                }
                count--;
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) { //判断是否都包含
        Boolean flag = true;
        for (Object o : c) {
            if(!contains(o)){
                flag = false;
            }
        }
        if(flag){
            return true;
        }
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        for (E e : c) {
            add(e);
        }
        return true;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        for (Object o : c) {
            if(!remove(o)){
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean retainAll(Collection<?> c) { //包含两个集合的元素
        ArrayList<Object> arrayList = new ArrayList<>();

            Boolean flag = true;
            for (Object o : c) {
                if(contains(o)){
                    arrayList.add(o);
                    flag = false;
                }
            }
            if(flag){
                return false;
            }
            data = arrayList.toArray();
            count = arrayList.size();
            return true;
    }

    @Override
    public void clear() {
        count = 0 ;
    }

    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("[");
        for (int i = 0 ; i<count;i++) {
            buffer.append(data[i]+" ");
        }
        buffer.append("]");
        return buffer.toString();
    }
}
